package com.yanqu.road.server.manger.activity.curio;

import com.yanqu.road.dao.impl.curio.CurioConfigDaoImpl;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.curio.CurioDrawControl;
import com.yanqu.road.entity.curio.CurioTempDrawConfig;
import com.yanqu.road.entity.curio.config.CurioBadgeInfo;
import com.yanqu.road.entity.curio.config.CurioDrawItem;
import com.yanqu.road.entity.curio.config.CurioItemInfo;
import com.yanqu.road.entity.curio.config.CurioItemSuitInfo;
import com.yanqu.road.entity.enums.eGoodsType;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.utils.string.StringUtils;

import java.util.*;

public class CurioConfig {


    /**
     * 藏品
     */
    private  Map<Integer, CurioItemInfo> itemInfoMap;

    /**
     * 抽奖池
     */
    private  List<CurioDrawItem> drawItemList;

    /**
     * 奖池保底
     */
    private  List<CurioDrawControl> drawControlList;

    /**
     * 藏品品质-兑换所需碎片数量
     */
    private  Map<Integer, Integer> exchangeCostMap;

    /**
     * 藏品套装
     */
    private  Map<Integer, CurioItemSuitInfo> suitInfoMap;

    /**
     * 等级品质评分
     */
    private  List<Integer> qualityScoreList;

    /**
     * 星级品质评分
     */
    private  List<Integer> starScoreList;

    /**
     * 勋章
     */
    private  List<CurioBadgeInfo> badgeInfoList;

    /**
     * 淘宝消耗
     */
    private  List<String> lotteryConsumeList;

    /**
     * 山河进度
     */
    private int schedule;

    /**
     * 山河进度-转化成道具
     */
    private String scheduleReward;

    /**
     * 聊天频道条件
     */
    private List<Integer> chatCondList;

    /**
     * 前10抽配置
     */
    private List<String> lottery10RewardList;
    
    public boolean init(){

        List<Integer> nowHideList = CurioConfigMgr.getHideGoods();

        //藏品
        Map<Integer, CurioItemInfo> tempItemInfoMap = new CurioConfigDaoImpl().getCurioItemInfo();
        tempItemInfoMap.entrySet().removeIf(integerCurioItemInfoEntry -> nowHideList.contains(integerCurioItemInfoEntry.getKey()));

        List<CurioDrawItem> tempDrawList = new ArrayList<>();

        //普通道具奖池
        List<String> tempList = StringUtils.stringToStringList(GameConfig.CURIO_LOTTERY_RULES_WEIGHTS, "\\|");
        for (String param : tempList) {
            List<String> valueParam =  StringUtils.stringToStringList(param, ";");
            tempDrawList.add(new CurioDrawItem(valueParam.get(0), Integer.parseInt(valueParam.get(1))));
        }
        CurioMgr.getLogger().info("====藏品====普通道具奖池数量：{}", tempList.size());

        //所有碎片
        List<GoodsInfo> exchangeList = GoodsMgr.getGoodsListByType(eGoodsType.CurioItemExchange.getValue());

        //碎片奖池
        List<String> fTempList = StringUtils.stringToStringList(GameConfig.CURIO_LOTTERY_RULES_WEIGHTS_FRAGMENTS, "\\|");
        calDrawItemWeight(tempItemInfoMap, tempDrawList, exchangeList, fTempList, false);

        //整卡奖池
        List<String> wTempList = StringUtils.stringToStringList(GameConfig.CURIO_LOTTERY_RULES_WEIGHTS_WHOLE_CARD, "\\|");
        calDrawItemWeight(tempItemInfoMap, tempDrawList, exchangeList, wTempList, true);

        Collections.shuffle(tempDrawList);

        //保底
        List<CurioDrawControl> controlList = new ArrayList<>();
        List<String> list = StringUtils.stringToStringList(GameConfig.CURIO_LOTTERY_RULES_GUARANTEE, "\\|");
        for (String str : list) {
            List<Integer> valueList = StringUtils.stringToIntegerList(str, ";");
            controlList.add(new CurioDrawControl(valueList.get(0), valueList.get(1)));
        }

        Map<Integer, Integer> tempExchangeCostMap = new HashMap<>();
        List<String> stringList = StringUtils.stringToStringList(GameConfig.CURIO_FRAGMENTS_COMPOSITE_RULES, "\\|");
        for (String str : stringList) {
            List<Integer> valueList = StringUtils.stringToIntegerList(str, ";");
            tempExchangeCostMap.put(valueList.get(0), valueList.get(1));
        }

        suitInfoMap = new CurioConfigDaoImpl().getCurioItemSuitInfo();
        badgeInfoList = new CurioConfigDaoImpl().getCurioBadgeInfoList();
        itemInfoMap = tempItemInfoMap;
        drawItemList = tempDrawList;
        drawControlList = controlList;
        exchangeCostMap = tempExchangeCostMap;
        qualityScoreList = StringUtils.stringToIntegerList(GameConfig.CURIO_QUALITY_SCORE, "\\|");
        starScoreList = StringUtils.stringToIntegerList(GameConfig.CURIO_STAR_SCORE, "\\|");
        lotteryConsumeList = StringUtils.stringToStringList(GameConfig.CURIO_LOTTERY_COST, "\\|");
        List<String> scheduleList = StringUtils.stringToStringList(GameConfig.CURIO_LOTTERY_SCHEDULE_REWARD, "#");
        schedule = Integer.parseInt(scheduleList.get(0));
        scheduleReward = scheduleList.get(1);

        chatCondList = StringUtils.stringToIntegerList(GameConfig.CURIO_CHAT_COND, "\\|");
        lottery10RewardList = StringUtils.stringToStringList(GameConfig.CURIO_LOTTERY_INIT_WHOLE_CARD, "\\|");
        if(lottery10RewardList.size() != 10){
            return false;
        }
        return true;
    }

    public void calDrawItemWeight(Map<Integer, CurioItemInfo> tempItemInfoMap, List<CurioDrawItem> tempDrawList, List<GoodsInfo> exchangeList, List<String> fTempList, boolean whole) {
        //品质-总权重
        Map<Integer, CurioTempDrawConfig> qualityWeigh = new HashMap<>();
        for (String param : fTempList) {
            List<String> paramList = StringUtils.stringToStringList(param, "=");
            //品质
            int quality = Integer.parseInt(paramList.get(0));
            List<Integer> numWightList = StringUtils.stringToIntegerList(paramList.get(1), ";");
            //数据
            int num = numWightList.get(0);
            //权重
            int weight = numWightList.get(1);
            CurioTempDrawConfig drawConfig = new CurioTempDrawConfig();
            drawConfig.setNum(num);
            drawConfig.setWeight(weight);

            for (GoodsInfo goodsInfo : exchangeList) {
                int itemId = goodsInfo.getParamList().get(0).intValue();
                CurioItemInfo itemInfo = tempItemInfoMap.get(itemId);
                if(itemInfo != null){
                    if(!whole) {
                        if (itemInfo.getQuality() == quality && itemInfo.getIsFragmentLottery() == 1) {
                            drawConfig.getGoodsInfoList().add(goodsInfo);
                        }
                    }else {
                        if (itemInfo.getQuality() == quality && itemInfo.getIsWholeCardLottery() == 1) {
                            drawConfig.getGoodsInfoList().add(goodsInfo);
                        }
                    }
                }
            }
            qualityWeigh.put(quality, drawConfig);
        }
        Set<Integer> keySet = new HashSet<>(qualityWeigh.keySet());
        int subWeight = 0;
        for (int quality : keySet) {
            CurioTempDrawConfig drawConfig = qualityWeigh.get(quality);
            //没有配置奖池的，权重要分摊给其他品质
            if(drawConfig.getGoodsInfoList().size() == 0){
                qualityWeigh.remove(quality);
                subWeight += drawConfig.getWeight();
            }
        }
        if(subWeight > 0 && qualityWeigh.size() > 0){
            int add = subWeight / qualityWeigh.size();
            for (Map.Entry<Integer, CurioTempDrawConfig> entry : qualityWeigh.entrySet()) {
                CurioTempDrawConfig value = entry.getValue();
                value.setWeight(value.getWeight() + add);
            }
        }

        for (Map.Entry<Integer, CurioTempDrawConfig> entry : qualityWeigh.entrySet()) {
            CurioTempDrawConfig value = entry.getValue();
            int size = value.getGoodsInfoList().size();
            for (GoodsInfo goodsInfo : value.getGoodsInfoList()) {
                CurioDrawItem drawItem = new CurioDrawItem(goodsInfo.getGoodsId() + "=" + value.getNum(), value.getWeight() / size);
                if(whole) {
                    drawItem.setWhole(true);
                    drawItem.setQuality(entry.getKey());
                    CurioMgr.getLogger().info("====藏品整卡：{}", drawItem.getReward());
                }else {
                    CurioMgr.getLogger().info("====藏品碎片：{}", drawItem.getReward());
                }
                tempDrawList.add(drawItem);
            }
        }
    }


    public List<Integer> getSuitList(int itemId){
        List<Integer> resultList = new ArrayList<>();
        for (Map.Entry<Integer, CurioItemSuitInfo> entry : suitInfoMap.entrySet()) {
            CurioItemSuitInfo value = entry.getValue();
            if(value.getCurioList().contains(itemId)){
                resultList.add(value.getId());
            }
        }
        return resultList;
    }

    public  long getLevelScore(int lv, int quality){
        long base = 0;
        if(quality < 0){
            return base;
        }
        if(quality <= qualityScoreList.size()){
            base = qualityScoreList.get(quality - 1);
        }else {
            base = qualityScoreList.get(qualityScoreList.size() - 1);
        }
        return base * lv;
    }

    public  long getStarScore(int lv, int quality){
        long base = 0;
        if(quality < 0){
            return base;
        }
        if(quality <= starScoreList.size()){
            base = starScoreList.get(quality - 1);
        }else {
            base = starScoreList.get(starScoreList.size() - 1);
        }
        return base * lv;
    }

    public  CurioBadgeInfo getBadgeInfo(int level){
        if(level > badgeInfoList.size()){
            return null;
        }
        for (int i = 0; i < badgeInfoList.size(); i++) {
            CurioBadgeInfo badgeInfo = badgeInfoList.get(i);
            if(badgeInfo.getLevel() == level){
                return badgeInfo;
            }
        }
        return null;
    }

    public CurioDrawItem getCurioDrawItemByReward(String reward){
        for (CurioDrawItem drawItem : drawItemList) {
            if(drawItem.getReward().equals(reward)){
                return drawItem;
            }
        }
        return null;
    }

    public  CurioItemInfo getCurioItemInfo(int id){
        return itemInfoMap.get(id);
    }

    public  CurioItemSuitInfo getCurioItemSuitInfo(int suitId){
        return suitInfoMap.get(suitId);
    }

    public  Map<Integer, CurioItemInfo> getItemInfoMap() {
        return itemInfoMap;
    }

    public  void setItemInfoMap(Map<Integer, CurioItemInfo> itemInfoMap) {
        this.itemInfoMap = itemInfoMap;
    }

    public  List<CurioDrawItem> getDrawItemList() {
        return drawItemList;
    }

    public  List<CurioDrawItem> getCopyDrawItemList() {
        return new ArrayList<>(drawItemList);
    }

    public  void setDrawItemList(List<CurioDrawItem> drawItemList) {
        this.drawItemList = drawItemList;
    }

    public  List<CurioDrawControl> getDrawControlList() {
        return drawControlList;
    }

    public  void setDrawControlList(List<CurioDrawControl> drawControlList) {
        this.drawControlList = drawControlList;
    }

    public  Map<Integer, Integer> getExchangeCostMap() {
        return exchangeCostMap;
    }

    public  void setExchangeCostMap(Map<Integer, Integer> exchangeCostMap) {
        this.exchangeCostMap = exchangeCostMap;
    }

    public  Map<Integer, CurioItemSuitInfo> getSuitInfoMap() {
        return suitInfoMap;
    }

    public  void setSuitInfoMap(Map<Integer, CurioItemSuitInfo> suitInfoMap) {
        this.suitInfoMap = suitInfoMap;
    }

    public  List<String> getLotteryConsumeList() {
        return lotteryConsumeList;
    }

    public int getSchedule() {
        return schedule;
    }

    public String getScheduleReward() {
        return scheduleReward;
    }

    public List<Integer> getChatCondList() {
        return chatCondList;
    }

    public List<String> getLottery10RewardList() {
        return lottery10RewardList;
    }
}
